﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class NewsContent : System.Web.UI.Page
{
    public string smallClassName, bigClassName, contentHtml, liHtml,newsTitle,addTime;
    string id = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null) { id = Request.QueryString["id"].ToString(); }
        if (IsNumeric(id) == false) { id = "1"; }

        //内容
        Hashtable htResult = ShowData("title,addtime,content,bigclassname,smallclassname,bigclassid,smallclassid", string.Format(@"
SELECT n.title,n.addtime,n.content, b.bigclassname, s.smallclassname,n.bigclassid,n.smallclassid
FROM web_notice n, web_notice_bigclass b, web_notice_smallclass s
WHERE n.bigclassid = b.id
AND n.smallclassid = s.id
AND n.id={0}", id));
        contentHtml = htResult["content"].ToString();
        bigClassName = htResult["bigclassname"].ToString();
        smallClassName = htResult["smallclassname"].ToString();
        newsTitle = htResult["title"].ToString();
        addTime = htResult["addtime"].ToString();
        string smallClassID = htResult["smallclassid"].ToString();
        string bigClassID = htResult["bigclassid"].ToString();

        //菜单
        DataSet dslink = Socut.Data.ExecuteDataSet(string.Format(@"
SELECT id,smallclassname 
FROM dbo.web_notice_smallclass 
WHERE bigclassid={0} 
ORDER BY smallclassorder", htResult["bigclassid"].ToString()));
        StringBuilder liBuilder = new StringBuilder();
        foreach (DataRow row in dslink.Tables[0].Rows)
        {
            int count = (int)Socut.Data.ExecuteScalar(string.Format(@"
select count(*) 
from web_notice  
where smallclassid = {0}", row["id"].ToString()));
            string goPage = string.Format("NewsList.aspx?bigclassid={0}&smallclassid={1}", bigClassID, row["id"].ToString());
            if (count == 1)
            {
                string contentId = Socut.Data.ExecuteScalar(string.Format(@"
select id
from web_notice  
where smallclassid = {0}", row["id"].ToString())).ToString();
                goPage = string.Format("NewsContent.aspx?id={0}", contentId);
            }
            if (row["id"].ToString() == smallClassID)
            {
                liBuilder.Append(string.Format("<li><a href=\"{0}\" class=\"content-left-current\">{1}</a></li>",
                    goPage, row["smallclassname"].ToString()));    
            }
            else
            {
                liBuilder.Append(string.Format("<li><a href=\"{0}\">{1}</a></li>",
                    goPage, row["smallclassname"].ToString()));
            }
        }
        liHtml = liBuilder.ToString();
    }

    private Hashtable ShowData(string fieldString,string sql)
    {
        Hashtable htResult = new Hashtable();
        Socut.Reader sqlDataReader = new Socut.Reader(sql);
        try
        {
            sqlDataReader.Read();

            string[] fieldList = fieldString.Split(',');
            foreach (string fieldName in fieldList)
            {
                htResult.Add(fieldName,htmlstr(sqlDataReader[fieldName].ToString()));
            }
        }
        catch(Exception ex)
        {
            Response.Write("为保证网站的服务质量，服务器临时维护中...请稍候!" + ex.Message);
            Response.End();
        }        

        sqlDataReader.Close();
        return htResult;
    }

    public string htmlstr(string chr)
    {
        if (chr == null)
            return "";
        chr = chr.Replace("\n", "<br>");
        chr = chr.Replace("select", "");
        chr = chr.Replace("delete", "");
        chr = chr.Replace("update", "");
        chr = chr.Replace("drop", "");
        chr = chr.Replace("and", "");
        chr = chr.Replace("or", "");
        return (chr);
    }

    static bool IsNumeric(string str)
    {
        if (str == null || str.Length == 0)
            return false;
        foreach (char c in str)
        {
            if (!Char.IsNumber(c))
            {
                return false;
            }
        }
        return true;
    }
}
